<?php


namespace app\admin1\controller;


use app\admin1\model\AddModel;
use app\admin1\validate\AddValidate;
use cmf\controller\AdminBaseController;

class AddController extends AdminBaseController
{
    //显示及页面
    public function index(){
//        $title = input('title','');
//        $where = [];
//        if ($title){
//            $where[] = ['title', 'like', "%$title%"];
//        }
        $list = AddModel::order(['id' => 'DESC'])
//            ->where($wehre)
            ->paginate(5);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //页面添加
    public function add(){
        return $this->fetch();
    }
    //添加方法
    public function addPost(){
        $data = input('post.','');
        $validate = new AddValidate();
        $res = $validate->scene('add')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
        if(AddModel::create([
            'consignee' => $data['consignee'],
            'mobile' => $data['mobile'],
            'area' => $data['area'],
            'address' => $data['address'],
        ])){
            $this->success('添加成功','index');
        }else{
            $this->error('添加失败');
        }
    }
}